<?php 
if(KT!='KhiThe') exit;
class scarts_controller extends scarts_model{
		
	public function __construct(){
		global $lang;
		$this->lang = $lang;
		parent::__construct();
		parent::addObject('khithe_template');
		parent::addObject('khithe_string');
		parent::addObject('khithe_request');
		parent::addObject('khithe_paging');
	}
	
	public function index_action()
	{
		$limit=25;
		$indent=NULL;
		$resultHtml = NULL;
		$totalRows = mysql_num_rows(parent::getSqlshoppingcarts('',''));
		$strPaging = $this->display_paging($totalRows, intval($this->valUrl(1)), intval($limit), url_base.'admin/scarts/index', $this->lang, '/');
		$this->lang['paging'] = $strPaging; 
		$this->lang['page'] = $this->valUrl(1);
		$strHtmlContent = $this->khithe_display('index');
		$arrBlockHtml = $this->khithe_getBlockHtml($strHtmlContent);
		$resultshoppingcart = parent::getSqlshoppingcarts(intval($this->valUrl(1)), intval($limit) );
		while( $rowshoppingcart = mysql_fetch_assoc($resultshoppingcart) )
		{
			if($rowshoppingcart['Process']=='process'){
				$process = $this->lang['process'];
			}else if($rowshoppingcart['Process']=='finished'){
				$process = $this->lang['finished'];
			}else{
				$process = $this->lang['notview'];
			}
			
			$arrshoppingcart = array
			(
				"cell_ID"	=>	$rowshoppingcart['ID'],
				"cell_1"	=>	'<input name="delId[]" type="checkbox" value="'.$rowshoppingcart['ID'].'" />',
				"cell_2"	=>	"SC_".$rowshoppingcart['ID'],
				"cell_3"	=>	$rowshoppingcart['Fullname'],
				"cell_4"	=>	$rowshoppingcart['Email'],
				"cell_5"	=>	($rowshoppingcart['Mobiphone']!=""?$rowshoppingcart['Mobiphone']:$rowshoppingcart['Telephone']),
				"cell_6"	=>	($rowshoppingcart['PostDate']=!""?date("d-m-Y",$rowshoppingcart['PostDate']):''),
				"cell_7"	=>	$process,
				"class_cell"=>	($rowshoppingcart['Status']=='true'?"cell_groupitem$indent":"cell_groupitem_ex"),				
			);
			
			$resultHtml .= $this->khithe_assign($arrBlockHtml['shoppingcart'],$arrshoppingcart);

		} mysql_free_result($resultshoppingcart);
		
		$strHtmlContent = $this->khithe_assignBlock($strHtmlContent, array('htmlshoppingcart'=>$resultHtml));
		$this->lang['dis'] ='';
		if(empty($totalRows)){
			$this->lang['dis'] = 'disabled="disabled"';
			$strHtmlContent .= '<h3 style="color:#F00">'.$this->lang['errnot'].'</h3>';
		}
		$strHtmlContent = $this->khithe_assign($strHtmlContent, $this->lang);
				
		return $strHtmlContent;
	}
	
	public function detail_action()
	{
		if($_GET['iframe']=='true')
		{
			$resultHtml = NULL;
			$item_total = 0;
			$strHtmlContent = $this->khithe_display('detail');
			$arrBlockHtml = $this->khithe_getBlockHtml($strHtmlContent);
			
			$resultList = parent::getListProduct(intval($this->valUrl(1)));
			while( $rowList = mysql_fetch_assoc($resultList) )
			{	
				$this->lang['dis_but'] ='';
				if($rowList['Process']=='process'){
					$process = $this->lang['process'];
				}else if($rowList['Process']=='finished'){
					$process = $this->lang['finished'];
					$this->lang['dis_but'] ='style="display:none"';
				}else{
					$process = $this->lang['notview'];
				}
				
				$arrList = array(
					"titleItem_1"=>$rowList['Fullname'],
					"titleItem_2"=>$rowList['Email'],
					"titleItem_3"=>$rowList['Telephone'],
					"titleItem_4"=>$rowList['Mobiphone'],
					"titleItem_5"=>$rowList['Address'],
					"titleItem_6"=>$rowList['CMND'],
					"titleItem_7"=>$rowList['PostDate']==''?'':date("d-m-Y",$rowList['PostDate']),
					"titleItem_8"=>$rowList['Bank']==0?$this->lang['money_cur']:$this->lang['money_bank'],
					"titleItem_9"=>$process,
					"titleItem_10"=>$rowList['Detail'],
					"titleItem_11"=>$rowList['Process'],
					"item_ID"=>$rowList['SID'],
					"item_1"=>$rowList['Title'],
					"item_2"=>$rowList['Qty'],
					"item_3"=>$rowList['Price']==''?0:number_format($rowList['Price'],0,'.',','),
					"item_4"=>($rowList['Qty']*$rowList['Price'])==''?0:number_format($rowList['Qty']*$rowList['Price'],0,'.',','),				
				);
				$item_total +=($rowList['Qty']*$rowList['Price']);
				$resultHtml .= $this->khithe_assign($arrBlockHtml['product_item'],$arrList);
				
			} mysql_free_result($resultList);
			
			$this->lang['dis'] = $this->valUrl(2)=='print'?'display:none':'display:block';
			$this->lang["item_total"] = $item_total==""?0:number_format($item_total,0,'.',',');		
			$strHtmlContent = $this->khithe_assignBlock($strHtmlContent, array('htmlPro'=>$resultHtml));
			$strHtmlContent = $this->khithe_assign($strHtmlContent, $this->lang);
			$strHtmlContent = $this->khithe_assign($strHtmlContent, $arrList);
			
			$Process =$arrList["titleItem_11"];
			if($Process == 'notview'){
				parent::setStatusCarts(intval($this->valUrl(1)),'process');
			}
			
			return $strHtmlContent;
		}
	}
	
	public function index_ajax_action()
	{

		switch ($this->valUrl(2))
		{
			case 'change':
				return parent::setStatusCarts(intval($this->valUrl(1)),'finished');
				
			break;
			
			case 'open':
				$arr = explode(',',$this->valUrl(3));
				foreach ( $arr as $ID ){
					$finish = parent::setCheckedshoppingcarts($ID,'true');
				}				
			break;
			
			case 'close':
				$arr = explode(',',$this->valUrl(3));
				foreach ( $arr as $ID ){
					$finish = parent::setCheckedshoppingcarts($ID,'false');
				}				
			break;
			
			case 'del':
				$arr = explode(',',$this->valUrl(3));
				foreach ( $arr as $ID ){
					$finish = parent::delCheckedshoppingcarts($ID);
				}				
			break;
			
		}
		
		if($finish)
			return $this->index_action();
		else
			return 'notOK';
	}
	


}
?>